<?php

namespace app\admin\controller\qingdongwms;

use addons\qingdongwms\model\Account;
use addons\qingdongwms\model\AreaManagement;
use addons\qingdongwms\model\Contacts;
use addons\qingdongwms\model\Contract;
use addons\qingdongwms\model\Customer;
use addons\qingdongwms\model\CustomerProduct;
use addons\qingdongwms\model\Field;
use addons\qingdongwms\model\Mobilecode;
use addons\qingdongwms\model\Parts;
use addons\qingdongwms\model\Product;
use addons\qingdongwms\model\Staff;
use app\admin\controller\qingdongwms\Base;
/**
 * 公共数据列表
 * @icon fa fa-circle-o
 */
class Common extends Base
{

    protected $noNeedRight = ['*'];
    /**
     * 客户
     */
    public function getCustomer()
    {   $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $customer = Customer::where(['owner_staff_id' => ['in', Staff::getMyStaffIds()]])->where($where)->field('id,name')->select();
        return json(['list' => $customer, 'total' => count($customer)]);
    }
    /**
     * 联系人
     */
    public function getContacts()
    {
        $customer_id = input('customer_id', 0);
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $where['customer_id'] = $customer_id;
        $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
        $contacts = Contacts::where($where)->field('id,name')->select();
        return json(['list' => $contacts, 'total' => count($contacts)]);
    }
    /**
     * 合同
     */
    public function getContract()
    {
        $customer_id = input('customer_id', 0);
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $where['customer_id'] = $customer_id;
        $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
        $contacts = Contract::where($where)->field('id,name,num')->select();
        $contacts = collection($contacts)->toArray();
        $data = [];
        foreach ($contacts as $v) {
            $data[] = ['id' => $v['id'], 'name' => $v['num'] . "（{$v['name']}）"];
        }
        return json(['list' => $data, 'total' => count($data)]);
    }
    /**
     * 区号
     */
    public function getCountry()
    {
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $countrys = Mobilecode::where($where)->field('id,name')->order('id desc')->select();
        return json(['list' => $countrys, 'total' => count($countrys)]);
    }

    /**
     * 地区
     */
    public function getManagement()
    {
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $countrys = AreaManagement::where($where)->field('id,name')->order('id desc')->select();
        return json(['list' => $countrys, 'total' => count($countrys)]);
    }
    /**
     * 产品
     */
    public function getProduct()
    {
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $countrys = Product::where($where)->field('id,name')->order('id desc')->select();
        return json(['list' => $countrys, 'total' => count($countrys)]);
    }
    /**
     * 员工
     */
    public function getStaff()
    {
        $where = ['status' => 1];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $staffs = Staff::where($where)->field('id,name')->order('id desc')->select();
        return json(['list' => $staffs, 'total' => count($staffs)]);
    }
    /**
     * 员工
     */
    public function get_staff()
    {
        $where = ['status' => 1];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $staffs = Staff::where($where)->field('id,name')->order('id desc')->select();
        return json(['list' => $staffs, 'total' => count($staffs)]);
    }
    /**
     * 产品价格
     */
    public function getProductPrice()
    {
        $id = input('id', 0);
        $price = Product::where(['id' => $id])->value('price') ?? 0;
        $this->success('请求成功', '', ['price' => $price]);
    }
    /**
     * 客户产品
     */
    public function getCustomerProduct()
    {
        $customer_id = input('customer_id', 0);
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $where['customer_id'] = $customer_id;
        $customerProduct = CustomerProduct::where($where)->with(['product'])->field('id,pay_date,product_id')->select();
        $list=[];
        foreach ($customerProduct as $v){
            $list[]=[
                'id'=>$v['id'],
                'name'=>$v['product']['name']."（{$v['pay_date']}）"
            ];
        }
        return json(['list' => $list, 'total' => count($list)]);
    }
    /**
     * 获取账户
     */
    public function getAccount()
    {
        $where = ['status'=>1];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }
        $accounts = Account::where($where)->select() ;
        return json(['list' => $accounts, 'total' => count($accounts)]);
    }
    /**
     * 获取备件
     */
    public function getParts()
    {
        $where = [];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['id'] = $keyValue;
        }

        $name       = input('name');
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        $parts=Parts::where(['status'=>'上架'])->field('id,name')->select();
        return json(['list' => $parts, 'total' => count($parts)]);
    }
    /**
     * 获取国外电话区号
     */
    public function getMobileCode() {
        $where =[];
        if ($keyValue = $this->request->request("keyValue")) {
            $where['number'] = $keyValue;
        }
        $number       = input('number');
        if(!empty($number)){
            $where['name|number'] = ['like','%'.$number.'%'];
        }
        $mobilecode = Mobilecode::where($where)->field('name,number')->orderRaw('CONVERT(name using gbk) asc')->select();

        return json(['list' => $mobilecode, 'total' => count($mobilecode)]);
    }
    /**
     * 获取字段
     */
    public function getField() {
        $name=input('name');
        $data=Field::getField($name);
        return json($data);
    }

}
